import type { IDictList } from '@/utils/dictionary';
import { defHttp } from '@/utils/http';
import { exc, excSocial, root } from './apiRoot';
import type {
  IAreaListResData,
  IUploadImgFilesResData,
  ISystemConfigRes,
  IUploadFilesResData,
  IMiniProgramCodeData,
  IAreaDataResultItem,
  BehaviorLikeStatusData,
  AppShareLogData,
  AppShareLogResult,
  IResData,
  TianyanchaSearchResult,
  GetDownStatisticsListResult,
  BehaviorBrowseSetResult,
  ClassTypeListKey,
  ClassTypeListResult,
  CreateURLlinkData,
  CustomFormItemTypeListResult,
  ResourceConfigResult,
  IBehaviorCollectListData,
  IBehaviorCollectOperateData,
  PublicityData,
} from './indexTypes';
import type { ThridTypeEnum } from '@/enums/loginEnum';
import type { RegisterIdAppUserInfo } from './activity/types';
import type { BehaviorCollectTypeEnum, BusinessFunctionTypeEnum } from '@/enums/publicModuleEnum';

export const Api = {
  getModules: exc + 'information/getModule',

  // 筛选相关数据
  areaList: root + 'districts/getList?pid=4403',
  currentService: root + 'uniapp/app/member/currentService',
  SYS_RATING: root + 'dict/data/SYS_RATING',
  downList: root + 'member/getDownList',
  dictAllList: root + 'dict/data/getList',
  fileUpload: 'system/dfs/upload',
  imgUpload: 'system/dfs/image_upload',
  systemConfig: 'config/downloadInfo',
  miniProgramCode: exc + 'api/getQRcode',
  submitWbsiteCode: excSocial + 'create',
  getAreaData: root + 'districts/getList',

  /** 点赞相关 */
  behaviorLikeStatus: root + 'behaviorLike/status',
  setBehaviorLike: root + 'behaviorLike',
  behaviorBrowseSet: root + 'behaviorBrowse/browseSet',

  /** 分享参数相关(所有分享从启动页进入, 部分分享内容存在云端) */
  addAppShareLog: root + 'app-share-log/add', // 添加分享内容
  getAppShareLog: root + 'app-share-log/exc/get', // 获取分享内容
  /** 天眼查搜索接口 */
  tianyanchaSearch: root + 'tianyancha/search',
  /** 天眼查表单回填内容 */
  tianyanchaDetail: root + 'tianyancha/detail',
  /** 学校搜索 */
  getSchoolList: root + 'school/getDownList',
  /** 学院搜索 */
  getCollegesList: root + 'school/colleges/getDownList',
  /** 专业搜索 */
  getMajorsList: root + 'school/specialty/getDownList',
  /** 天眼查快照 */
  tianyanchaDetailSnapshot: root + 'tianyancha/detailSnapshot',
  /** 天眼查综合评分 */
  tianyanchaSelectScore: root + 'tianyancha/selectScore',
  /** 获取会员等级(职务)列表, 带会员总数 */
  getDownStatisticsList: root + 'memberLevel/getDownStatisticsList',
  getClassTypeList: root + 'memberType/getClassTypeList',

  createURLlink: root + 'app-share-log/generateUrlLink',

  readSign: root + 'messageRead/readSign',
  mallSecretKeyApi: root + 'mall/login/getSecretKey',
  customFormItemTypeList: root + 'custom-form/itemTypeList',

  /** 获取微信手机号码 */
  getWechatPhoneNumber: root + 'getWechatPhoneNumber',
  /**用户完善资料度查询 */
  selectUserPerfectInfoProgress: root + 'app-user-info/selectUserPerfectInfoProgress',
  /** 资源导航 ==> 资源入口列表 */
  orgResourceConfigList: root + 'org-resource-config/list',
  /** 我的收藏列表 */
  behaviorCollectList: root + 'behaviorCollect/list',
  /** 收藏/取消收藏 */
  behaviorCollectOperate: root + 'behaviorCollect/operate',
  /** 获取业务中的宣传页已读状态 */
  getTypePublicity: root + 'app-user-publicity-record/getTypePublicity',
  /** 设置业务中宣传页已读 */
  setPublicity: root + 'app-user-publicity-record/addPublicity',
  /** 业务模块推广记录新增 */
  boUserPromotionCountDataAdd: root + 'bo-user-promotion-count-data/add'
};
/**
 * 筛选 - 会员单位 - 获取区域数据
 * @export
 * @return {*}
 */
export function areaList(): Promise<IAreaListResData[]> {
  return defHttp.get({
    url: Api.areaList,
  });
}

/**
 *  用户完善资料度查询
 * @returns
 */
export function selectUserPerfectInfoProgress(appUserInfoId?: number): Promise<RegisterIdAppUserInfo> {
  return defHttp.get({
    url: Api.selectUserPerfectInfoProgress,
    data: { appUserInfoId }
  });
}

/**
 * 筛选 - 会员单位 - 获取服务人列表
 * @export
 * @return {*}  {Promise<any>}
 */
export function currentService(): Promise<any> {
  return defHttp.get({
    url: Api.currentService,
  });
}

/**
 * 筛选 - 会员单位 - 获取等级列表
 * @export
 * @return {*}  {Promise<any>}
 */
export function SYS_RATING(): Promise<any> {
  return defHttp.get({
    url: Api.SYS_RATING,
  });
}

/**
 * 筛选 - 会员单位 - 获取下拉的筛选数据
 * @export
 * @param {string} type
 * @return {*}  {Promise<any>}
 */
export function downList(type?: string): Promise<any> {
  return defHttp.get({
    url: Api.downList + `/${type}`,
  });
}

/**
 * 获取字典
 * @export
 * @param {string} dictTypes 字典key | 多个key逗号隔开
 * @return {IDictList}
 */
export function dictAllList(dictTypes?: string): Promise<IDictList> {
  return defHttp.get({
    url: Api.dictAllList,
    data: {
      dictTypes,
    },
  });
}

interface UploadImgFiles {
  <T = IUploadImgFilesResData>(files: TempFile): Promise<T>;
  <T = IUploadImgFilesResData>(files: TempFile[]): Promise<T[]>;
}
/**
 * 上传图片
 * @export
 * @param {(TempFile[] | TempFile)} files
 */
export const uploadImgFiles: UploadImgFiles = function (files: TempFile[] | TempFile) {
  return defHttp.files({
    url: Api.imgUpload,
    files: files as any,
  });
};

interface UploadFiles {
  <T = IUploadFilesResData>(files: TempFile): Promise<T>;
  <T = IUploadFilesResData>(files: TempFile[]): Promise<T[]>;
}

/**
 * 上传文件
 * @export
 * @param {(TempFile[] | TempFile)} files
 */
export const fileUpload: UploadFiles = function (files: TempFile[] | TempFile) {
  return defHttp.files({
    url: Api.fileUpload,
    files: files as any,
  });
};

/**
 * 获取系统配置
 * @export
 * @return {*}
 */
export function systemConfig(keys: string = 'sys.home'): Promise<ISystemConfigRes> {
  return defHttp.get({
    url: Api.systemConfig,
    data: {
      keys,
    },
  });
}

/**
 * 获取小程序二维码
 * @export
 * @param {IMiniProgramCodeData} data
 * @return {*} {Promise<string>}
 */
export function miniProgramCode(data: IMiniProgramCodeData): Promise<string> {
  return defHttp.post({
    url: Api.miniProgramCode,
    data,
  });
}

/**
 * 提交从官网二维码中获取的code到后台服务
 * @export
 * @param {string} code
 * @return {*}
 */
export function submitWbsiteCode(code: string): Promise<any> {
  return defHttp.post({
    url: Api.submitWbsiteCode + `/${code}`,
  });
}

/**
 * @description 获取省市区数据
 * @date 05/08/2022
 * @export
 * @param {number} [pid=0] 上级id
 */
export function getAreaData(pid: number = 0) {
  return defHttp.get<IAreaDataResultItem[]>({
    url: Api.getAreaData,
    data: { pid },
  });
}

/**
 * @description 获取点赞状态
 * @date 15/08/2022
 * @export
 * @param {BehaviorLikeStatusData} data
 */
export function behaviorLikeStatus(data: BehaviorLikeStatusData) {
  return defHttp.get<boolean>({
    url: Api.behaviorLikeStatus,
    data,
  });
}

/**
 * @description 设置点赞状态
 * @date 15/08/2022
 * @export
 * @param {({ status: 0 | 1 } & BehaviorLikeStatusData)} data
 */
export function setBehaviorLike(data: { status: 0 | 1 } & BehaviorLikeStatusData) {
  return defHttp.post({
    url: Api.setBehaviorLike,
    data,
  });
}

/**
 * @description 获取浏览记录
 * @date 29/11/2022
 * @export
 * @param {BehaviorLikeStatusData} data
 */
export function behaviorBrowseSet(data: BehaviorLikeStatusData) {
  return defHttp.get<IResData<BehaviorBrowseSetResult>>({
    url: Api.behaviorBrowseSet,
    data,
  });
}

/**
 * @description 添加一条分享日志, 将分享参数存入云端
 * @date 18/10/2022
 * @export
 * @param {AppShareLogData} data
 */
export function addAppShareLog(data: AppShareLogData) {
  return defHttp.post<AppShareLogResult>({
    url: Api.addAppShareLog,
    data,
  });
}

/**
 * @description 获取分享内容
 * @date 18/10/2022
 * @export
 * @param {number} id
 */
export function getAppShareLog(id: number) {
  return defHttp.get<AppShareLogResult>({
    url: Api.getAppShareLog + `/${id}`,
  });
}

/**
 * @description 天眼查搜索接口
 * @date 07/11/2022
 * @export
 * @param {string} keyword
 */
export function tianyanchaSearch(keyword: string) {
  return defHttp.get<IResData<TianyanchaSearchResult>>({
    url: Api.tianyanchaSearch,
    data: {
      pageNum: 1,
      pageSize: 10,
      keyword,
    },
  });
}

/**
 * @description 学校搜索接口
 * @date 24/04/2023
 * @export
 * @param {string} keyword
 */
export function getSchoolList(keyword: string) {
  return defHttp.get<IResData<{ id: number, name: string }>>({
    url: Api.getSchoolList,
    data: {
      pageNum: 1,
      pageSize: 10,
      keyword,
    }
  })
}

/**
 * @description 学院搜索接口
 * @date 21/05/2024
 * @export
 * @param {string} keyword
 */
export function getCollegesList(keyword: string) {
  return defHttp.get<IResData<{ id: number, name: string }>>({
    url: Api.getCollegesList,
    data: {
      pageNum: 1,
      pageSize: 10,
      keyword,
    }
  })
}

/**
 * @description 专业搜索接口
 * @date 21/05/2024
 * @export
 * @param {string} keyword
 */
export function getMajorsList(keyword: string) {
  return defHttp.get<IResData<{ id: number, name: string }>>({
    url: Api.getMajorsList,
    data: {
      pageNum: 1,
      pageSize: 10,
      keyword,
    }
  })
}


/**
 * @description 天眼查表单回填内容
 * @date 09/11/2022
 * @export
 * @param {TianyanchaSearchResult} data
 */
export function tianyanchaDetail(data: TianyanchaSearchResult) {
  return defHttp.post<Recordable>({
    url: Api.tianyanchaDetail,
    data,
  });
}

/**
 * @description 获取天眼查快照
 * @date 11/11/2022
 * @export
 * @param {number} id
 * @param {boolean} [force] 是否更新
 */
export function tianyanchaDetailSnapshot(id: number, force?: true) {
  return defHttp.get(
    {
      url: Api.tianyanchaDetailSnapshot,
      data: {
        id,
        force,
      },
    },
    {
      isRequestTransform: true,
    },
  );
}

/**
 * @description 天眼查综合评分
 * @date 14/11/2022
 * @export
 * @param {number} id
 */
export function tianyanchaSelectScore(id: number) {
  return defHttp.get<{ percentileScore: number }>({
    url: Api.tianyanchaSelectScore,
    data: { id },
  });
}

/**
 * @description 获取会员等级(职务)列表, 带会员总数
 * @date 24/11/2022
 * @export
 */
export function getDownStatisticsList() {
  return defHttp.get<GetDownStatisticsListResult[]>({
    url: Api.getDownStatisticsList,
  });
}

/**
 * @description 获取公共分类
 * @date 16/02/2023
 * @export
 * @param {ClassTypeListKey[]} keyword
 */
export function getClassTypeList(keyword: ClassTypeListKey[]) {
  return defHttp.get<ClassTypeListResult[]>({
    url: Api.getClassTypeList + `/${keyword.join(',')}`,
  });
}

/**
 * @description 生成小程序URL Link
 * @date 05/08/2024
 * @export
 * @param {CreateURLlinkData} data
 */
export function createURLlink(data: CreateURLlinkData) {
  return defHttp.post<string>({
    url: Api.createURLlink,
    data
  })
}

/**
 * @description 短信消息已读上报
 * @date 19/05/2023
 * @export
 * @param {string} readId
 */
export function readSign(readId: string) {
  return defHttp.get({
    url: Api.readSign,
    data: { readId }
  })
}

/**
 * @description 获取登录商城的key
 * @date 04/11/2023
 * @export
 */
export function getMallSecretKeyApi() {
  return defHttp.get<string>({
    url: Api.mallSecretKeyApi
  })
}

/**
 * @description 获取表单项类型列表
 * @date 26/03/2024
 * @export
 */
export function customFormItemTypeList() {
  return defHttp.get<CustomFormItemTypeListResult[]>({
    url: Api.customFormItemTypeList
  })
}

/**
 * @description 获取微信授权手机号码
 * @date 22/07/2024
 * @export
 * @param {{ phoneCode: string, iv: string, encryptedData: string }} data
 */
export function getWechatPhoneNumber(data: { phoneCode: string, iv: string, encryptedData: string, thridType: ThridTypeEnum }) {
  return defHttp.post<string>({
    url: Api.getWechatPhoneNumber,
    data
  })
}

/**
 * @description 资源导航 ==> 资源入口列表
 * @date 09/12/2024
 * @export
 */
export function orgResourceConfigList() {
  return defHttp.get<ResourceConfigResult[]>({
    url: Api.orgResourceConfigList
  })
}

/**
 * @description 我的收藏列表
 * @date 11/12/2024
 * @export
 */
export function behaviorCollectList(data: IBehaviorCollectListData) {
  return defHttp.post<IResData<any>>({
    url: Api.behaviorCollectList,
    data
  })
}

/**
 * @description 收藏/取消收藏
 * @date 11/12/2024
 * @export
 * @param {IBehaviorCollectOperateData} data
 */
export function behaviorCollectOperate(data: IBehaviorCollectOperateData) {
  return defHttp.post<boolean>({
    url: Api.behaviorCollectOperate,
    data
  })
}

/**
 * @description 获取业务中的宣传页已读状态
 * @date 09/01/2025
 * @export
 * @param {{ type: 'bo' }} data
 */
export function getTypePublicity(data: PublicityData) {
  return defHttp.post<boolean>({
    url: Api.getTypePublicity,
    data
  })
}

/**
 * @description 设置业务中宣传页已读
 * @date 09/01/2025
 * @export
 * @param {PublicityData} data
 */
export function setPublicity(data: PublicityData) {
  return defHttp.post<boolean>({
    url: Api.setPublicity,
    data
  })
}

/**
 * @description 业务模块推广记录新增
 * @date 09/01/2025
 * @export
 * @param {{ type: BusinessFunctionTypeEnum, dataId: number }} data
 */
export function boUserPromotionCountDataAdd(data: { type: BusinessFunctionTypeEnum, dataId: number }) {
  return defHttp.post<boolean>({
    url: Api.boUserPromotionCountDataAdd,
    data
  })
}